package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 大攻略资源管理
 */

@RestController
@RequestMapping("strategies")
public class StrategyController {

    //关联service
    @Autowired
    private IStrategyService strategyService;
    //关联service
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    //关联service
    @Autowired
    private IStrategyCommentService strategyCommentService;

    //获取满足一定条件的所有大攻略,用pageInfo的形式返回,方便页面循环,不然还要包一层
    @GetMapping
    public PageInfo list(@ModelAttribute("qo") StrategyQueryObject qo){
        return  strategyService.query(qo);
    }
    //根据id获取对应的大攻略
    @GetMapping("{id}")
    public Strategy getById(@PathVariable Long id){
        return  strategyService.getById(id);
    }

    //获取对应id的大攻略下面的分类攻略
    @GetMapping("{strategyId}/catalogs")
    public List<StrategyCatalog> listByStrategyId(@PathVariable Long strategyId){
        return  strategyCatalogService.listByStrategyId(strategyId);
    }

    //获取对应id的大攻略下面的评论
    @GetMapping("{strategyId}/comments")
    public PageInfo queryByStrategyId(StrategyCommentQueryObject qo){
        qo.setOrderby("sc.createTime desc");
        return  strategyCommentService.query(qo);
    }

    //保存评论,保存到某一个大攻略id下面去
    @PostMapping("{strategy.id}/comments")
    public JSONResult saveOrUpdate(StrategyComment comment){//上面的id这样写可以自动封装到comment对象的strategy属性里的id上
        comment.setUser(UserContext.getCurrentUser());//设置评论者
        strategyCommentService.saveOrUpdate(comment,comment.getTags());
        return  new JSONResult();
    }
}
